package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.admin.mapper.RoomInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    RoomInfoMapper roomInfoMapper;
    @Autowired
    ApartmentInfoMapper apartmentInfoService;
    @Autowired
    GraphInfoService graphInfoService;
    @Autowired
    AttrValueService attrValueService;
    @Autowired
    FacilityInfoService facilityInfoService;
    @Autowired
    LabelInfoService labelInfoService;
    @Autowired
    PaymentTypeService paymentTypeService;
    @Autowired
    LeaseTermService leaseTermService;
    @Autowired
    RoomAttrValueService roomAttrValueService;
    @Autowired
    RoomFacilityService roomFacilityService;
    @Autowired
    RoomLabelService roomLabelService;
    @Autowired
    RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    RoomLeaseTermService roomLeaseTermService;

    @Override
    public IPage<RoomItemVo> getPageItemByRoomQueryVo(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.getPageItemByRoomQueryVo(page, queryVo);
    }

    @Override
    public RoomDetailVo getRoomDetailById(Long id) {
        RoomInfo roomInfo = this.getById(id);
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        ApartmentInfo apartmentInfo = apartmentInfoService.selectById(roomInfo.getApartmentId());
        List<GraphVo> graphVo = graphInfoService.getGraphVoByRoomId(id);
        List<AttrValueVo> attrValueVoList = attrValueService.getAttrValueVoByRoomId(id);
        List<FacilityInfo> facilityInfoList = facilityInfoService.getFacilityInfoByRoomId(id);
        List<LabelInfo> labelInfoList = labelInfoService.getLabelListByRoomId(id);

        List<PaymentType> paymentTypeList = paymentTypeService.getPaymentTypeByRoomId(id);
        List<LeaseTerm> leaseTermList = leaseTermService.getLeaseTermByRoomId(id);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVo);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        return roomDetailVo;
    }

    @Override
    public void saveOrUpdateByRoomSubmitVo(RoomSubmitVo roomSubmitVo) {
        Long id = roomSubmitVo.getId();
        this.saveOrUpdate(roomSubmitVo);
        if (id != null) {
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().
                    eq(GraphInfo::getItemId, id).
                    eq(GraphInfo::getItemType, ItemType.ROOM));
            roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, id));

            roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, id));
            roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, id));
            roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, id));
            roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId, id));

        }
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        List<GraphInfo> graphInfoList = new ArrayList<>();
        graphVoList.forEach(graphVo -> {
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setName(graphVo.getName());
            graphInfo.setItemType(ItemType.ROOM);
            graphInfo.setItemId(roomSubmitVo.getId());
            graphInfo.setUrl(graphVo.getUrl());
            graphInfoList.add(graphInfo);
        });
        graphInfoService.saveBatch(graphInfoList);

        List<RoomAttrValue> roomAttrValues = new ArrayList<>();
        roomSubmitVo.getAttrValueIds().stream().forEach(attrs -> {
            RoomAttrValue roomAttrValueBuilder = RoomAttrValue.builder().
                    roomId(roomSubmitVo.getId()).attrValueId(attrs).build();
            roomAttrValues.add(roomAttrValueBuilder);
        });

        roomAttrValueService.saveBatch(roomAttrValues);

        List<RoomFacility> roomFacilities=new ArrayList<>();
        roomSubmitVo.getFacilityInfoIds().stream().forEach(fis->{
            RoomFacility build = RoomFacility.builder().facilityId(fis).roomId(roomSubmitVo.getId()).build();
            roomFacilities.add(build);
        });
        roomFacilityService.saveBatch(roomFacilities);

        List<RoomLabel> roomLabels=new ArrayList<>();
        roomSubmitVo.getLabelInfoIds().stream().forEach(lii->{
            RoomLabel build = RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(lii).build();
            roomLabels.add(build);
        });
        roomLabelService.saveBatch(roomLabels);

        List<RoomPaymentType> list=new ArrayList<>();
        roomSubmitVo.getPaymentTypeIds().stream().forEach(pti->{
            RoomPaymentType build = RoomPaymentType.builder().roomId(roomSubmitVo.getId()).
                    paymentTypeId(pti).
                    build();
            list.add(build);
        });
        roomPaymentTypeService.saveBatch(list);

        List<RoomLeaseTerm> leaseTermList=new ArrayList<>();
        roomSubmitVo.getLeaseTermIds().forEach(lti->{
            RoomLeaseTerm build = RoomLeaseTerm.builder().
                    roomId(roomSubmitVo.getId()).
                    leaseTermId(lti).build();
            leaseTermList.add(build);
        });
        roomLeaseTermService.saveBatch(leaseTermList);
    }
}




